<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-12
 * Time: 14:18
 */

namespace App\Http\Controllers\Admin;

use App\Base\BaseFunc;
use App\Base\Power;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class AdminController extends Controller
{
    public function adminLogin()
    {
        return view("admin.adminLogin");
    }

    public function adminLogins(BaseFunc $baseFunc)
    {
        $input = Input::only('employee_id');
        $password = Input::only('employee_password');
        $input['employee_password'] = md5($password['employee_password']);
        $employee = DB::table('employee')
            ->where('employee_id',"=",$input['employee_id'])
            ->value('employee_password');
        if($employee == NULL)
        {
            $baseFunc ->setRedirectMessage(false,"您还没有注册或没有通过审核，请注册",NULL,"/adminRegister");
        }
        else
        {
            $employeeData = DB::table('employee')
                ->where('employee_id',"=",$input['employee_id'])
                ->where('employee_password',"=",$input['employee_password'])
                ->first();
            if($employeeData == NULL)
            {
                $baseFunc ->setRedirectMessage(false,"密码错误",NULL,"/adminLogin");
            }
            else
            {
                if($employeeData->employee_state == 0)
                {
                    $baseFunc ->setRedirectMessage(false,"您提交的信息正在审核中，请稍后登录",NULL,"/adminLogin");
                }
                else
                {
                    $laozong = DB::table('power')
                        ->where("power_name","=","老总")
                        ->first();
                    $jingli = DB::table('power')
                        ->where("power_name","=","经理")
                        ->first();
                    $duizhang = DB::table('power')
                        ->where("power_name","=","队长")
                        ->first();
                    $yuangong = DB::table('power')
                        ->where("power_name","=","员工")
                        ->first();
                    if($employeeData->employee_power == $laozong->power_id)
                    {
                        session(['employee_id' => $employeeData->employee_id]); //获取登录员工的ID
                        session(['employee_name' => $employeeData->employee_name]); //获取登录员工的名字
                        session(['employee_power' => $laozong->power_name]); //获取登录员工的职位名称
                        $baseFunc ->setRedirectMessage(true,"登录成功，欢迎来到成都家政服务系统",NULL,"/searchEmployee");
                    }
                    elseif($employeeData->employee_power == $jingli->power_id)
                    {
                        session(['employee_id' => $employeeData->employee_id]); //获取登录员工的ID
                        session(['employee_name' => $employeeData->employee_name]); //获取登录员工的名字
                        session(['employee_phone' => $employeeData->employee_phone]); //获取登录员工的电话
                        session(['employee_area' => $employeeData->employee_area]); //获取登录员工的区域ID
                        session(['employee_state' => $employeeData->employee_state]); //获取登录员工的状态
                        session(['employee_power_id' => $employeeData->employee_power]); //获取登录员工的职位ID
                        session(['employee_power' => $jingli->power_name]); //获取登录员工的职位名称
                        $baseFunc ->setRedirectMessage(true,"登录成功，欢迎来到成都家政服务系统",NULL,"/areaMaster.searchEmployee");
                    }
                    elseif($employeeData->employee_power == $duizhang->power_id)
                    {
                        session(['employee_id' => $employeeData->employee_id]); //获取登录员工的ID
                        session(['employee_name' => $employeeData->employee_name]); //获取登录员工的名字
                        session(['employee_phone' => $employeeData->employee_phone]); //获取登录员工的电话
                        session(['employee_area' => $employeeData->employee_area]); //获取登录员工的区域ID
                        session(['employee_state' => $employeeData->employee_state]); //获取登录员工的状态
                        session(['employee_power_id' => $employeeData->employee_power]); //获取登录员工的职位ID
                        session(['employee_power' => $duizhang->power_name]); //获取登录员工的职位名称
                        $baseFunc ->setRedirectMessage(true,"登录成功，欢迎来到成都家政服务系统",NULL,"/start");
                    }
                    elseif($employeeData->employee_power == $yuangong->power_id)
                    {
                        session(['employee_id' => $employeeData->employee_id]); //获取登录员工的ID
                        session(['employee_name' => $employeeData->employee_name]); //获取登录员工的名字
                        session(['employee_phone' => $employeeData->employee_phone]); //获取登录员工的电话
                        session(['employee_area' => $employeeData->employee_area]); //获取登录员工的区域ID
                        session(['employee_state' => $employeeData->employee_state]); //获取登录员工的状态
                        session(['employee_power_id' => $employeeData->employee_power]); //获取登录员工的职位ID
                        session(['employee_power' => $yuangong->power_name]); //获取登录员工的职位名称
                        $baseFunc ->setRedirectMessage(true,"登录成功，欢迎来到成都家政服务系统",NULL,"/employee.searchAreaTask");
                    }
                }
            }
        }
    }

    public function adminLogout(BaseFunc $baseFunc)
    {
        session(["employee_id"=>NULL]);
        $baseFunc ->setRedirectMessage(true,"您已退出登录",NULL,"/adminLogin");
    }

    public function adminRegister()
    {
        $data['area'] = DB::table('area') -> get();
        return view("admin.adminRegister",$data);
    }

    public function adminRegisters(BaseFunc $baseFunc,Power $power)
    {
        $employeeData = Input::only('employee_id','employee_name','employee_sex','employee_phone','employee_age','employee_area','employee_password','employee_info');
        $employeeData['employee_password'] = md5($employeeData['employee_password']);
        $employeeData['employee_state'] = 0;
        $employeeData['employee_power'] = $power->yuangong();
        $employeeData['employee_time'] = date("Y-m-d H:i:s");
        $check = DB::table('employee')
            ->where('employee_id','=',$employeeData['employee_id'])
            ->get();
        if($check)
        {
            $baseFunc->setRedirectMessage(false,"此身份证号已被使用，请重新注册",NULL,"/adminRegister");
        }
        else
        {
            DB::table('employee')->insert($employeeData);
            $baseFunc->setRedirectMessage(true,"已提交申请，正在审核中，请稍后尝试登录",NULL,"/adminLogin");
        }
    }
}